use actix_web::{HttpResponse, web};

use common::error::MyError;
use common::r::JsonResult;
use entity::user;
use model::user::{CreateUser, ResetPassword, SetUerRole, UserPage};
use service::manager::user::UserManager;
use service::user::UserService;

use crate::controller::base::BaseController;

pub struct UserController {}

impl BaseController<UserService, i64, user::Model, UserPage, CreateUser, user::Model, UserManager>
    for UserController
{
}

impl UserController {
    pub async fn reset_password(form: web::Json<ResetPassword>) -> Result<HttpResponse, MyError> {
        let res = UserService::reset_password(form.into_inner()).await?;
        Ok(HttpResponse::Ok().json(JsonResult::ok(res)))
    }

    pub async fn get_role(params: web::Path<i64>) -> Result<HttpResponse, MyError> {
        let res = UserService::get_role(params.into_inner()).await?;
        Ok(HttpResponse::Ok().json(JsonResult::ok(res)))
    }

    pub async fn set_role(form: web::Json<SetUerRole>) -> Result<HttpResponse, MyError> {
        let res = UserService::set_role(form.into_inner()).await?;
        Ok(HttpResponse::Ok().json(JsonResult::ok(res)))
    }
}
